package com.mcf.xiaolinju.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mcf.xiaolinju.entity.Dynamic;
import com.mcf.xiaolinju.entity.Tagmessage;
import com.mcf.xiaolinju.mapper.DynamicMapper;
import com.mcf.xiaolinju.service.IDynamicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcf.xiaolinju.vo.DynamicVO;
import com.mcf.xiaolinju.vo.DynamicbasicVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcf
 * @since 2022-02-20
 */
@Service
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper, Dynamic> implements IDynamicService {
 @Resource
 DynamicMapper dynamicMapper;
 @Resource
 TagmessageServiceImpl tagmessageService;
    public String addapi(String content, int createid, String pictureurl, int communityid) {
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

        if(dynamicMapper.addapi(content,createid,pictureurl,communityid,ldt)>0){
            return "发表成功,需要等待管理员审核";
        }else {
            return "发表失败";
        }
    }
    //小程序查询动态的信息
    public List<DynamicVO> selectallDynamic(int id,int userid) {
     List<DynamicVO> dynamicVOList= new ArrayList<>();

        List<DynamicbasicVO> dynamicbasicVOList=new ArrayList<>();
        dynamicbasicVOList=dynamicMapper.selectallDynamic(id);
        for(DynamicbasicVO d:dynamicbasicVOList){
            DynamicVO dynamicVO=new DynamicVO();
            Tagmessage one = tagmessageService.getOne(Wrappers.<Tagmessage>lambdaQuery().eq(Tagmessage::getDynamicid,d.getId())
                    .eq(Tagmessage::getTagsid,userid));
            if(one!=null){
                dynamicVO.setTagstate(1);
            }
            else{
                dynamicVO.setTagstate(0);
            }

            dynamicVO.setId(d.getId());
            dynamicVO.setContent(d.getContent());
            dynamicVO.setState(d.getState());
            dynamicVO.setCreateid(d.getCreateid());
            dynamicVO.setAuditid(d.getAuditid());
            dynamicVO.setCreatetime(d.getCreatetime());
            dynamicVO.setAudittime(d.getAudittime());
            dynamicVO.setPraisecount(d.getPraisecount());
            dynamicVO.setReviewcount(d.getReviewcount());
            dynamicVO.setCommunityid(d.getCommunityid());
            dynamicVO.setUsername(d.getUsername());
            String [] Provinces=d.getPictureurl().split(";");
            dynamicVO.setPicture(Arrays.asList(Provinces));
            dynamicVO.setPortrait(d.getPortrait());
            dynamicVOList.add(dynamicVO);

        }
        return  dynamicVOList;
    }

    public DynamicVO selectoneDynamic(int id,int keyid) {
       DynamicVO dynamicVO= new DynamicVO();


            DynamicbasicVO d=dynamicMapper.selectoneDynamic(id,keyid);
            dynamicVO.setId(d.getId());
            dynamicVO.setContent(d.getContent());
            dynamicVO.setState(d.getState());
            dynamicVO.setCreateid(d.getCreateid());
            dynamicVO.setAuditid(d.getAuditid());
            dynamicVO.setCreatetime(d.getCreatetime());
            dynamicVO.setAudittime(d.getAudittime());
            dynamicVO.setPraisecount(d.getPraisecount());
            dynamicVO.setReviewcount(d.getReviewcount());
            dynamicVO.setCommunityid(d.getCommunityid());
            dynamicVO.setUsername(d.getUsername());
            String [] Provinces=d.getPictureurl().split(";");
            dynamicVO.setPicture(Arrays.asList(Provinces));
            dynamicVO.setPortrait(d.getPortrait());



        return  dynamicVO;
    }
}
